package com.sunday.fangdaifu.activity.task;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.reflect.TypeToken;
import com.sunday.common.base.BaseFragment;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.adapter.TaskListAdapter;
import com.sunday.fangdaifu.base.MyApplication;
import com.sunday.fangdaifu.common.Api;
import com.sunday.fangdaifu.common.ApiClient;
import com.sunday.fangdaifu.common.ApiServiceImpl;
import com.sunday.fangdaifu.model.Task;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/7/3.
 */

public class TaskListFragment extends BaseFragment implements ApiServiceImpl.HttpResponseInterface {


    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private int type;
    private int status;
    private int userId;
    private int pageNo = 1;
    private int pageSize = 10;
    private List<Task> taskList = new ArrayList<>();
    private TaskListAdapter adapter;


    public static TaskListFragment newInstance(int type, int status) {
        TaskListFragment fragment = new TaskListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putInt("status", status);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type", 0);
            status = getArguments().getInt("status", 0);
        }
    }

    public void setType(int type) {
        this.type = type;
        adapter.setType(type);
}

    public void updateData() {
        pageNo = 1;
        getData();

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.layout_task_list, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        userId = MyApplication.getInstance().getMember().getId();
        initRecyclerView();
        getData();
    }

    private LinearLayoutManager layoutManager;
    private int lastVisibleItem;
    private boolean isCanloadMore;

    private void initRecyclerView() {
        adapter = new TaskListAdapter(mContext, taskList,type);
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).drawable(R.drawable.shape_divider).build());
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE
                        && lastVisibleItem + 1 == adapter.getItemCount() && isCanloadMore) {
                    getData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();

            }
        });

    }

    private void getData() {
        ApiClient.getApiService().taskList(userId, type, status, pageNo, pageSize, this, new TypeToken<ResultDO<List<Task>>>() {
        }.getType());
    }

    @Override
    public void onResponce(String key, Object result) {
        if (isFinish) {
            return;
        }
        switch (key) {
            case Api.TASK_LIST:
                ResultDO<List<Task>> resultDO = (ResultDO<List<Task>>) result;
                if (resultDO.getCode() == 0) {
                    if (pageNo == 1) {
                        taskList.clear();
                    }
                    if (resultDO.getResult() == null)
                        return;
                    taskList.addAll(resultDO.getResult());
                    if (resultDO.getResult().size() == 10) {
                        pageNo++;
                        isCanloadMore = true;
                    } else {
                        isCanloadMore = false;
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showToast(mContext, resultDO.getMessage() + "");
                }
                break;
        }
    }

    @Override
    public void onFailure() {
//        dissMissDialog();
        ToastUtils.showToast(mContext, "网络连接错误");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
